//
//  GMainTabBar.swift
//  sinaWeiBoAI
//
//  Created by Z.J on 16/3/20.
//  Copyright © 2016年 Z.J. All rights reserved.
//

import UIKit
// TabBarItem个数
let TabBarItemCount = 5

class GMainTabBar: UITabBar {

    
    override func layoutSubviews()
    {
        super.layoutSubviews()
        
        // 算出每个item的宽度
        let itemWidth = frame.width / CGFloat(TabBarItemCount)
        // 定义索引
        var index = 0
        // 循环来设置frame值
        for view in subviews
        {
            // 取出class类型 通过 字符串
            let cls:AnyClass? = NSClassFromString("UITabBarButton")
            // 判断,如果属于这个类
            if view.isKindOfClass(cls!)
            {
                // 设置每个UITabBarButton的位置
                view.frame = CGRect(x: CGFloat(index) * itemWidth, y: 0, width: itemWidth, height: bounds.height)
                
                index++
                
                if(index == 2)
                {
                    composeButton.frame = CGRect(x: CGFloat(index) * itemWidth, y: 0, width: itemWidth, height: bounds.height)
                    
                    index++
                }
            }
        }

    
    }
    
    // MARK: - 懒加载
    
    private lazy var composeButton: UIButton = {
        let button = UIButton()

        // 设置
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        // 添加到tabBar上面
        self.addSubview(button)
        
        return button
    }()
    
}
